<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 临床应用字典
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.labelNm" placeholder="应用科室名称" class="handle-input mr10"
                          @keyup.enter.native="handleSearch" style="width: 180px"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
                <div class="right-items" style="float: right;" v-if="roleId!=='view'">
                    <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
                </div>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column prop="labelId" label="应用科室编号" align="center"></el-table-column>
                <el-table-column prop="labelNm" label="应用科室名称" align="center"></el-table-column>
                <el-table-column prop="createdAccount" label="创建人" align="center"></el-table-column>
                <el-table-column prop="createdTime" label="创建时间" align="center"></el-table-column>
                <el-table-column prop="labelRmk" label="说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center" v-if="roleId!=='view'">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="20%" :before-close="closeAdd">
            <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="110px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="应用科室名称:" prop="labelNm">
                            <el-input v-model="addForm.labelNm"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="说明:" prop="labelRmk">
                            <el-input   type="textarea" v-model="addForm.labelRmk"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeAdd">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="20%" :before-close="closeEdit">
            <el-form ref="editForm" :rules="editFormRules" :model="editForm" label-width="110px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="应用科室名称:" prop="labelNm">
                            <el-input v-model="editForm.labelNm"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="说明:" prop="labelRmk">
                            <el-input   type="textarea" v-model="editForm.labelRmk"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {deptmentData, doAdd, selectBylabelId, doUpdate, doDelete} from '../api/deptment';

    export default {
        name: 'user',
        data() {
            return {
                roleId:localStorage.getItem('ms_roleId'),
                query: {
                    labelNm: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                editVisible: false,
                addVisible: false,
                pageTotal: 0,
                addForm: {},
                editForm: {},
                idx: -1,
                id: -1,
                addFormRules: {
                    labelNm: [
                        {required: true, message: '请输入应用科室名称', trigger: 'blur'},
                    ],
                },
                editFormRules: {
                    labelNm: [
                        {required: true, message: '请输入应用科室名称', trigger: 'blur'},
                    ],
                },
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取后台数据
            getData() {
                deptmentData(this.query).then(res => {
                    if (res.success == true) {
                        this.tableData = res.data;
                        this.pageTotal = res.total;
                    } else {
                        this.$message.error("查询失败！");
                        this.tableData = [];
                        this.pageTotal = 0;
                    }
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.labelNm = "";
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        //后台进行删除操作
                        doDelete({"labelId": row.labelId}).then(res => {
                            if (res.success) {
                                this.$message.success('删除成功');
                                this.getData();
                            } else {
                                this.$message.error(res.data);
                                this.getData();
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            message: '已取消删除'
                        });
                    });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 新增操作
            handleAdd(index, row) {
                this.addForm = {};
                this.addVisible = true;
            },
            /*新增用户*/
            saveAdd() {
                this.$refs['addForm'].validate((valid) => {
                    if (valid) {
                        console.log("新增的应用科室：---", this.addForm)
                        //新增form表单提交到后台
                        doAdd(this.addForm).then(res => {
                            if (res.data == 1) {
                                this.addVisible = false;
                                this.addForm = {};
                                this.getData();
                                this.$message.success('保存成功');
                            } else {
                                this.$message.error('保存失败');
                            }
                        });
                    }
                });
            },
            closeAdd() {//关闭新增弹出框
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.addVisible = false;
                        this.addForm = {};
                        this.getData();
                    })
                    .catch(() => {
                        this.$message({
                            message: '已取消操作'
                        });
                    });
            },
            // 编辑操作
            async handleEdit(index, row) {
                this.idx = index;
                await selectBylabelId({labelId: row.labelId}).then(res => {
                    if (res.success == true) {
                        this.editForm = res.data;
                        this.editVisible = true;
                    } else {
                        this.$message.error('查询失败');
                    }
                });
            },
            closeEdit() {//关闭编辑弹出框
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.editVisible = false;
                        this.editForm = {};
                        this.getData();
                    })
                    .catch(() => {
                        this.$message({
                            message: '已取消操作'
                        });
                    });
            },
            // 保存编辑
            saveEdit() {
                this.$refs['editForm'].validate((valid) => {
                    if (valid) {   //form表单提交到后台
                        console.log("编辑的应用科室：---", this.editForm);
                        doUpdate(this.editForm).then(res => {
                            if (res.data == 1) {
                                this.editVisible = false;
                                this.editForm = {};
                                this.getData();
                                this.$message.success('编辑成功');
                            } else {
                                this.$message.error('编辑失败');
                            }
                        });
                    }
                });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
        /*border-radius 属性允许您为元素添加圆角边框！*/
        border-radius: 6px;
        background-clip: padding-box;
        border-top-left-radius: 6px;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
        border-bottom-left-radius: 6px;
    }

    /*头像上传*/
    .user-header {
        position: relative;
        display: inline-block;
    }

    .user-header-com {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        display: inline-block;
    }

    .header-upload-btn {
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
        /* 通过定位把input放在img标签上面，通过不透明度隐藏 */
    }
</style>
